<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class CouponAtvRecord extends \app\BaseModel
{
    protected $name = "massage_service_coupon_atv_record";
    protected $append = ["coupon"];
    public function getCouponAttr($value, $data)
    {
        if(!empty($data["id"])) {
            $list_model = new CouponAtvRecordCoupon();
            $dis = ["a.record_id" => $data["id"], "a.user_id" => 0];
            $list = $list_model->alias("a")->join("massage_service_coupon b", "a.coupon_id = b.id")->where($dis)->where("b.status", ">", -1)->field("a.*,b.title,b.stock,b.i")->group("a.coupon_id")->select()->toArray();
            return $list;
        }
    }
    public function couponIsAtv($coupon_id)
    {
        $dis_where[] = ["status", "=", 1];
        $dis_where[] = ["end_time", "<", time()];
        $this->dataUpdate($dis_where, ["status" => 3]);
        $dis = ["a.status" => 1, "b.coupon_id" => $coupon_id];
        $data = $this->alias("a")->join("massage_service_coupon_atv_record_coupon b", "a.id = b.record_id")->where($dis)->count();
        return !empty($data) ? true : false;
    }
    public function dataAdd($data)
    {
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function shIng($cap_id)
    {
        $dis = ["cap_id" => $cap_id, "status" => 1];
        $count = $this->where($dis)->count();
        return $count;
    }
    public function dataList($dis, $page = 10)
    {
        $data = $this->where($dis)->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function couponCount($user_id)
    {
        $dis[] = ["user_id", "=", $user_id];
        $dis[] = ["status", "=", 1];
        $dis[] = ["end_time", ">", time()];
        $data = $this->where($dis)->count();
        return $data;
    }
}

?>